package com.taxi.driver.module.order.list;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.qianxx.view.xrecyclerview.IXRecyclerViewListener;
import com.qianxx.view.xrecyclerview.XRecyclerView;
import com.taxi.driver.R;
import com.taxi.driver.common.BaseFragment;
import com.taxi.driver.module.order.detail.OrderDetailActivity;
import com.taxi.driver.module.order.list.dagger.DaggerOrdreListComponent;
import com.taxi.driver.module.order.list.dagger.OrderListModule;
import com.taxi.driver.module.vo.OrderSummaryVO;
import com.taxi.driver.util.Navigate;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 功能描述：订单列表
 */
public class OrderListFragment extends BaseFragment implements OrderListContract.View {

    @BindView(R.id.tv_empty)
    TextView mTvEmpty;
    @BindView(R.id.x_recycler_view)
    XRecyclerView mXRecyclerView;

    private OrderListAdapter mAdapter;
    private View mBottomView;

    public static OrderListFragment newInstance() {
        return new OrderListFragment();
    }

    @Inject
    OrderListPresenter mPresenter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_order_list, container, false);
        ButterKnife.bind(this, mView);

        initView();
        initListener();

        return mView;
    }

    private void initView() {
        mAdapter = new OrderListAdapter(getContext());
        mAdapter.setDriverType(mPresenter.driverType());
        mXRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mXRecyclerView.setAdapter(mAdapter);

        mAdapter.setOnItemClickListener((position, view, item) -> mPresenter.reqOrderDetail(item.uuid));

        mBottomView = LayoutInflater.from(getContext()).inflate(R.layout.fragment_order_list_bottom, mXRecyclerView, false);
    }

    private void initListener() {
        mXRecyclerView.setXRecyclerViewListener(new IXRecyclerViewListener() {
            @Override
            public void onRefresh() {
                mPresenter.refresh();
            }

            @Override
            public void onLoadMore() {
                mPresenter.loadMore();
            }
        });
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        DaggerOrdreListComponent.builder()
                .appComponent(getAppComponent())
                .orderListModule(new OrderListModule(this))
                .build().inject(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        mPresenter.subscribe();
        mXRecyclerView.onRefresh();
    }

    @Override
    public void onStop() {
        super.onStop();
        mPresenter.unsubscribe();
    }

    @Override
    public void setList(List<OrderSummaryVO> vos) {
        mAdapter.setAll(vos);
        mXRecyclerView.refreshComplete();
        if (vos.size() <= 0) {
            mXRecyclerView.setLoadHide();
        }
    }

    @Override
    public void appendList(List<OrderSummaryVO> vos) {
        if (vos.size() < 10) {
            mAdapter.addFooterView(mBottomView);
            mXRecyclerView.setLoadComplete(true);
        } else {
            mXRecyclerView.setLoadComplete(false);
        }
        mAdapter.addAll(vos);
    }

    /**
     * 跳转到进行中订单
     *
     * @param orderUuid
     */
    @Override
    public void openOrderOngoing(String orderUuid) {
        Navigate.openOrderOngoing(getContext(), orderUuid);
    }

    /**
     * 跳转到订单详情
     *
     * @param orderUuid
     */
    @Override
    public void openOrderDetail(String orderUuid,Integer state) {
        OrderDetailActivity.start(getActivity(), orderUuid,state);
    }

    @Override
    public void reassign() {
        mPresenter.refresh();
    }
}
